package br.com.mundodaprata.store.model;

import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Persistence;
import javax.persistence.Table;

@NamedQueries({@NamedQuery(name="cityByName", query="select c from City c where c.name like :name")})
@Entity
@Table
public class City extends AbstractEntity {

	private static final long serialVersionUID = 1L;

	@Id
	@Column
	private Long code;

	@Column(nullable = false)
	private String name;

	@Column(nullable = false)
	private String state;

	@Column(name = "zip_code", nullable = false)
	private String zipCode;

	@Enumerated(EnumType.STRING)
	@Column(name = "type_city")
	private TypeCity typeCity;
	
	@ManyToOne
	@JoinColumn(name = "major", 
	            referencedColumnName = "code")
	private City major;
	
	@OneToMany(mappedBy = "major")
	private List<City> dependents;
	
	@OneToMany(mappedBy = "city")
	private List<Neighborhood> neighborhoods;
	
	public List<Neighborhood> getNeighborhoods() {
		return neighborhoods;
	}
	
	public void setNeighborhoods(List<Neighborhood> neighborhoods) {
		this.neighborhoods = neighborhoods;
	}
	
	public List<City> getDependents() {
		return dependents;
	}
	
	public void setDependents(List<City> dependents) {
		this.dependents = dependents;
	}

	public Long getCode() {
		return code;
	}

	public void setCode(Long code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getZipCode() {
		return zipCode;
	}

	public void setZipCode(String zipCode) {
		this.zipCode = removeFormat(zipCode);
	}
	
	private String removeFormat(String zipCode){
		return zipCode.replaceAll("-", "");		
	}

	public TypeCity getTypeCity() {
		return typeCity;
	}

	public void setTypeCity(TypeCity typeCity) {
		this.typeCity = typeCity;
	}

	public City getMajor() {
		return major;
	}

	public void setMajor(City major) {
		this.major = major;
	}
	
	@Override
	public String toString() {
		StringBuilder result = new StringBuilder();
		
		result.append(getCode())
		      .append(" - ")
		      .append(getName());
		
		return result.toString();
	}

	public static void main(String[] args) {
		EntityManagerFactory emf = 
			Persistence.createEntityManagerFactory("store");
		
		EntityManager em = emf.createEntityManager();
		
		City found = em.find(City.class, 9560L);
		System.out.println(found.getName());
		List<Neighborhood> neighborhoods = 
							found.getNeighborhoods();
		for (Neighborhood neighborhood : neighborhoods) {
			System.out.println(neighborhood.getName());
		}

//		City city = new City();
//		city.setCode(3L);
//		city.setName("Santa Cruz das Posses");
//		city.setState("SP");
//		city.setZipCode("15990320");
//		city.setTypeCity(TypeCity.D);
//		city.setMajor(em.find(City.class, 9560L));
//		
//		System.out.println(city.getMajor().getName());
//		
//		try {
//			em.getTransaction().begin();
//			em.persist(city);
//			em.getTransaction().commit();
//		} catch (Exception e) {
//			if(em.getTransaction().isActive()){
//				em.getTransaction().rollback();
//			}
//		}
	}

}